// 激光类
import { TimeManager } from './TimeManager.js';

export class Laser {
    constructor(startX, startY, endX, endY, type = 'enemy') {
        this.startX = startX;
        this.startY = startY;
        this.endX = endX;
        this.endY = endY;
        this.type = type;
        
        // 激光属性
        this.width = 20; // 激光宽度
        this.damage = 2; // 激光伤害
        this.life = 1200; // 激光持续时间（毫秒）
        this.maxLife = this.life;
        
        // 视觉特效
        this.intensity = 1.0;
        this.pulsePhase = 0;
        
        // 碰撞检测用的线段
        this.length = Math.sqrt((endX - startX) ** 2 + (endY - startY) ** 2);
        this.angle = Math.atan2(endY - startY, endX - startX);
        
        // 生成激光路径上的碰撞点
        this.collisionPoints = this.generateCollisionPoints();
    }
    
    generateCollisionPoints() {
        const points = [];
        const step = 5; // 每5像素一个碰撞点
        const steps = Math.floor(this.length / step);
        
        for (let i = 0; i <= steps; i++) {
            const t = i / steps;
            const x = this.startX + (this.endX - this.startX) * t;
            const y = this.startY + (this.endY - this.startY) * t;
            points.push({ x, y });
        }
        
        return points;
    }
    
    update(deltaTime) {
        // 更新激光生命周期
        this.life = TimeManager.updateTimer(this.life, deltaTime);
        
        // 更新强度（淡出效果）
        this.intensity = this.life / this.maxLife;
        
        // 更新脉冲效果
        this.pulsePhase = TimeManager.updatePhase(this.pulsePhase, deltaTime, 20);
        
        // 返回激光是否仍然存在
        return this.life > 0;
    }
    
    render(ctx) {
        if (this.life <= 0) return;
        
        ctx.save();
        
        // 计算激光的脉冲效果
        const pulse = 0.8 + Math.sin(this.pulsePhase) * 0.2;
        const alpha = this.intensity * pulse;
        
        // 绘制激光外层光晕
        const glowGradient = ctx.createLinearGradient(
            this.startX - this.width, this.startY,
            this.startX + this.width, this.startY
        );
        glowGradient.addColorStop(0, `rgba(255, 0, 0, 0)`);
        glowGradient.addColorStop(0.5, `rgba(255, 100, 100, ${alpha * 0.3})`);
        glowGradient.addColorStop(1, `rgba(255, 0, 0, 0)`);
        
        ctx.strokeStyle = glowGradient;
        ctx.lineWidth = this.width + 12; // 增加光晕宽度（从8增加到12）
        ctx.lineCap = 'round';
        ctx.beginPath();
        ctx.moveTo(this.startX, this.startY);
        ctx.lineTo(this.endX, this.endY);
        ctx.stroke();
        
        // 绘制激光主体
        ctx.strokeStyle = `rgba(255, 50, 50, ${alpha * 0.9})`;
        ctx.lineWidth = this.width;
        ctx.beginPath();
        ctx.moveTo(this.startX, this.startY);
        ctx.lineTo(this.endX, this.endY);
        ctx.stroke();
        
        // 绘制激光核心
        ctx.strokeStyle = `rgba(255, 255, 255, ${alpha})`;
        ctx.lineWidth = this.width * 0.4; // 从0.3增加到0.4
        ctx.beginPath();
        ctx.moveTo(this.startX, this.startY);
        ctx.lineTo(this.endX, this.endY);
        ctx.stroke();
        
        // 绘制激光粒子效果
        this.renderLaserParticles(ctx, alpha);
        
        ctx.restore();
    }
    
    renderLaserParticles(ctx, alpha) {
        // 在激光路径上绘制粒子效果
        const particleCount = Math.floor(this.length / 10);
        
        for (let i = 0; i < particleCount; i++) {
            const t = Math.random();
            const x = this.startX + (this.endX - this.startX) * t;
            const y = this.startY + (this.endY - this.startY) * t;
            
            // 随机偏移
            const offsetX = (Math.random() - 0.5) * this.width * 0.5;
            const offsetY = (Math.random() - 0.5) * this.width * 0.5;
            
            ctx.fillStyle = `rgba(255, 200, 100, ${alpha * Math.random() * 0.8})`;
            ctx.beginPath();
            ctx.arc(x + offsetX, y + offsetY, Math.random() * 3 + 1, 0, Math.PI * 2);
            ctx.fill();
        }
    }
    
    // 检查与矩形对象的碰撞
    checkCollision(obj) {
        if (this.life <= 0) return false;
        
        // 如果是玩家对象且有getCollisionBox方法，使用精确碰撞盒
        let targetX, targetY, targetWidth, targetHeight;
        if (obj.getCollisionBox && typeof obj.getCollisionBox === 'function') {
            const collisionBox = obj.getCollisionBox();
            targetX = collisionBox.x;
            targetY = collisionBox.y;
            targetWidth = collisionBox.width;
            targetHeight = collisionBox.height;
        } else {
            // 使用对象的默认尺寸
            targetX = obj.x;
            targetY = obj.y;
            targetWidth = obj.width;
            targetHeight = obj.height;
        }
        
        // 使用线段与矩形的碰撞检测
        return this.lineRectIntersect(
            this.startX, this.startY, this.endX, this.endY,
            targetX, targetY, targetX + targetWidth, targetY + targetHeight
        );
    }
    
    // 线段与矩形相交检测
    lineRectIntersect(x1, y1, x2, y2, minX, minY, maxX, maxY) {
        // 检查线段端点是否在矩形内
        if ((x1 >= minX && x1 <= maxX && y1 >= minY && y1 <= maxY) ||
            (x2 >= minX && x2 <= maxX && y2 >= minY && y2 <= maxY)) {
            return true;
        }
        
        // 检查线段是否与矩形的四条边相交
        return this.lineIntersect(x1, y1, x2, y2, minX, minY, maxX, minY) || // 上边
               this.lineIntersect(x1, y1, x2, y2, maxX, minY, maxX, maxY) || // 右边
               this.lineIntersect(x1, y1, x2, y2, minX, maxY, maxX, maxY) || // 下边
               this.lineIntersect(x1, y1, x2, y2, minX, minY, minX, maxY);   // 左边
    }
    
    // 两条线段相交检测
    lineIntersect(x1, y1, x2, y2, x3, y3, x4, y4) {
        const denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
        if (denom === 0) return false;
        
        const t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom;
        const u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denom;
        
        return t >= 0 && t <= 1 && u >= 0 && u <= 1;
    }
}